package com.ruoyi.business.service.impl;

import java.util.List;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.business.domain.WmsInventoryPlan;
import com.ruoyi.business.domain.WmsInventoryRecord;
import com.ruoyi.business.mapper.WmsInventoryPlanMapper;
import com.ruoyi.business.mapper.WmsInventoryRecordMapper;
import com.ruoyi.common.utils.DateUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.WmsInventoryRecordDetailMapper;
import com.ruoyi.business.domain.WmsInventoryRecordDetail;
import com.ruoyi.business.service.IWmsInventoryRecordDetailService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 盘点单货物信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-21
 */
@Service
public class WmsInventoryRecordDetailServiceImpl implements IWmsInventoryRecordDetailService
{
    @Autowired
    private WmsInventoryRecordDetailMapper wmsInventoryRecordDetailMapper;

    @Autowired
    private WmsInventoryPlanMapper wmsInventoryPlanMapper;

    @Autowired
    private WmsInventoryRecordMapper wmsInventoryRecordMapper;

    /**
     * 查询盘点单货物信息
     *
     * @param id 盘点单货物信息主键
     * @return 盘点单货物信息
     */
    @Override
    public WmsInventoryRecordDetail selectWmsInventoryRecordDetailById(Long id)
    {
        return wmsInventoryRecordDetailMapper.selectWmsInventoryRecordDetailById(id);
    }

    /**
     * 查询盘点单货物信息列表
     *
     * @param wmsInventoryRecordDetail 盘点单货物信息
     * @return 盘点单货物信息
     */
    @Override
    public List<WmsInventoryRecordDetail> selectWmsInventoryRecordDetailList(WmsInventoryRecordDetail wmsInventoryRecordDetail)
    {
        return wmsInventoryRecordDetailMapper.selectWmsInventoryRecordDetailList(wmsInventoryRecordDetail);
    }

    /**
     * 新增盘点单货物信息
     *
     * @param wmsInventoryRecordDetail 盘点单货物信息
     * @return 结果
     */
    @Override
    public int insertWmsInventoryRecordDetail(WmsInventoryRecordDetail wmsInventoryRecordDetail)
    {
        wmsInventoryRecordDetail.setCreateTime(DateUtils.getNowDate());
        return wmsInventoryRecordDetailMapper.insertWmsInventoryRecordDetail(wmsInventoryRecordDetail);
    }

    /**
     * 修改盘点单货物信息
     *
     * @param wmsInventoryRecordDetail 盘点单货物信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateWmsInventoryRecordDetail(WmsInventoryRecordDetail wmsInventoryRecordDetail)
    {
        wmsInventoryRecordDetail.setUpdateTime(DateUtils.getNowDate());
        int i = wmsInventoryRecordDetailMapper.updateWmsInventoryRecordDetail(wmsInventoryRecordDetail);
        String inventoryNo = wmsInventoryRecordDetail.getInventoryNo();
        // 作业状态
        List<JSONObject> list = wmsInventoryRecordDetailMapper.countNullNum(inventoryNo);
        if (ObjectUtils.isNotEmpty(list)) {
            for (JSONObject object : list) {
                int c1 = object.getIntValue("c1", 0);
                int c2 = object.getIntValue("c2", 0);
                boolean c3 = object.getBooleanValue("c3", false);
                String status = "wait";
                if (c3) {
                    status = "all";
                } else if (c2 > 0) {
                    status = "partial";
                }
                WmsInventoryRecord record = new WmsInventoryRecord();
                record.setInventoryNo(inventoryNo);
                record.setFactoryId(object.getLong("factory_id"));
                record.setStoreId(object.getLong("store_id"));
                record.setStoreRegionId(object.getLong("store_region_id"));
                record.setStoreRegionLocationId(object.getLong("store_region_location_id"));
                record.setStatus(status);
                wmsInventoryRecordMapper.updateStatus(record);
            }
        }
        // 计划状态
        List<JSONObject> list1 = wmsInventoryRecordDetailMapper.queryStatus(inventoryNo);
        if (ObjectUtils.isNotEmpty(list1)) {
            for (JSONObject object : list1) {
                String status = object.getString("status");
                Long planId = object.getLong("plan_id");
                if (status != null) {
                    WmsInventoryPlan plan = new WmsInventoryPlan();
                    plan.setId(planId);
                    plan.setStatus(status);
                    wmsInventoryPlanMapper.updateWmsInventoryPlan(plan);
                }
            }
        }
        return i;
    }

    /**
     * 批量删除盘点单货物信息
     *
     * @param ids 需要删除的盘点单货物信息主键
     * @return 结果
     */
    @Override
    public int deleteWmsInventoryRecordDetailByIds(Long[] ids)
    {
        return wmsInventoryRecordDetailMapper.deleteWmsInventoryRecordDetailByIds(ids);
    }

    /**
     * 删除盘点单货物信息信息
     *
     * @param id 盘点单货物信息主键
     * @return 结果
     */
    @Override
    public int deleteWmsInventoryRecordDetailById(Long id)
    {
        return wmsInventoryRecordDetailMapper.deleteWmsInventoryRecordDetailById(id);
    }
}
